package ui.face {
	
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	import globals.Global;
	
	public class Bar extends Sprite {
		
		private var _current:Number;
		private var _max:Number;
		private var track:Bitmap;
		private var bg:Bitmap;
		private var _mask:Shape;

		public function Bar(trackUrl:String, bgUrl:String) {
			super();
			
			bg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData(bgUrl));
			addChild(bg);
			track = new Bitmap(Global.assetsManager.getAssetsAsBitmapData(trackUrl));
			track.x = (bg.width - track.width) / 2;
			track.y = (bg.height - track.height) / 2;
			addChild(track);
			
			_mask = new Shape();
			addChild(_mask);
			track.mask = _mask;
		}
		
		public function update(value:Number, max:Number = -1):void {
			if (max != -1) {
				_max = max;
			}
			_current = Math.min(value, _max);
			updateBar();
		}
		
		private function updateBar():void {
			_mask.graphics.clear();
			_mask.graphics.beginFill(0xFFFFFF);
			_mask.graphics.drawRect(track.x, track.y, _current / _max * track.width, track.height);
			
		}
		
		public function clear():void {
			bg.bitmapData = track.bitmapData = null;
		}
	}
}